package com.lucun.es_transaction.service.impl;

import com.lucun.es_transaction.entity.UserInfo;
import com.lucun.es_transaction.entity.UserLog;
import com.lucun.es_transaction.mapper.UserInfoMapper;
import com.lucun.es_transaction.mapper.UserLogMapper;
import com.lucun.es_transaction.service.UserInfoService;
import com.lucun.es_transaction.util.BaseException;
import com.lucun.es_transaction.util.JsonResult;
import com.lucun.es_transaction.util.JwtUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;


@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    UserLogMapper userLogMapper;
    /*
     *  添加用户个人信息
     **/
    @Override
    public JsonResult<?> insert(UserInfo record,HttpServletRequest request) {
        if (ObjectUtils.isEmpty(record)){
            throw new BaseException("没有获取数据");
        }
        String tokenNumber = JwtUtil.getNumberByToken(request);
        if (StringUtils.isBlank(tokenNumber)){
            throw new BaseException("解析tokenNumber失败");
        }
        int id = JwtUtil.getIdByToken(request);
        if (ObjectUtils.isEmpty(id)){
            throw new BaseException("解析token失败");
        }
        String sex = record.getSex();
        if (StringUtils.isBlank(sex)){
            throw new BaseException("性别不能为空");
        }
        int age = record.getAge();
        if (ObjectUtils.isEmpty(age)){
            throw new BaseException("年龄不能为空");
        }
        String email = record.getEmail();
        if (StringUtils.isBlank(email)){
            throw new BaseException("邮箱不能为空");
        }

        String introduction = record.getIntroduction();
        if (StringUtils.isBlank(introduction)){
            throw new BaseException("简介不能为空");
        }
        record.setNumber(tokenNumber);
        record.setCreateTime(new Date());

        //实例化
        UserInfo userInfo = new UserInfo();
        //赋值number
        userInfo.setNumber(tokenNumber);
        //通过唯一的number查询
        UserInfo userInfo1 = userInfoMapper.selectAllByAll(userInfo);
        if(!ObjectUtils.isEmpty(userInfo1)){
            throw new BaseException("该用户已添加过信息，不能重复添加");
        }
        UserLog log=new UserLog();
        log.setLogName("账号:"+tokenNumber+"添加了个人信息");
        log.setLogType((byte)1);
        log.setCreateTime(new Date());
        log.setUserId(1);
        int i = userInfoMapper.insertSelective(record);
        if (i>0){
            log.setLogStatus((byte)1);
        }else {
            log.setLogStatus((byte)0);
        }
        userLogMapper.insert(log);
        return  new JsonResult<> ("添加个人信息成功!");
    }

    /*
     *  通过编号查询
     **/

    @Override
    public UserInfo findByNumber( HttpServletRequest request) {
        String tokenNumber = JwtUtil.getNumberByToken(request);
        if (StringUtils.isBlank(tokenNumber)){
            throw new BaseException("解析number失败");
        }

        return userInfoMapper.findByNumber(tokenNumber);
    }

    /*
     *  修改用户个人信息
     **/
    @Override
    public JsonResult<?> updateByPrimaryKeySelective(UserInfo record,HttpServletRequest r) {
        if (ObjectUtils.isEmpty(record)){
            throw new BaseException("没有获取数据");
        }
        String Number = JwtUtil.getNumberByToken(r);
        if (StringUtils.isBlank(Number)){
            throw new BaseException("解析number失败");
        }
        record.setUpdateTime(new Date());
        record.setNumber(Number);
        int i = userInfoMapper.updateByPrimaryKeySelective(record);
        UserLog log=new UserLog();
        log.setLogName(Number+"修改了用户个人信息");
        log.setLogType((byte)3);
        log.setCreateTime(new Date());
        log.setUserId(1);
        if (i>0){
            log.setLogStatus((byte)1);
        }else {
            log.setLogStatus((byte)0);
        }
        userLogMapper.insert(log);
        return  new JsonResult<> ("修改成功!");
    }
    /*
     * 修改认证状态
     * */
    @Override
    public JsonResult<?> update(UserInfo userInfo,HttpServletRequest request) {
        Integer id = userInfo.getId();
        if (ObjectUtils.isEmpty(id)) {
            throw new BaseException("没有获取id");
        }
        int idByToken = JwtUtil.getIdByToken(request);
        if(ObjectUtils.isEmpty(idByToken)){
            throw new BaseException("解析token失败");
        }
        userInfo.setUpdateTime(new Date());
        int i = userInfoMapper.update(id);
        UserLog log = new UserLog();
        log.setCreateTime(new Date());
        log.setLogType((byte) 3);
        log.setUserId(idByToken);
        if(i>0) {
            log.setLogName(idByToken+"修改成功");
            log.setLogStatus((byte) 1);
            userLogMapper.insertSelective(log);
        }else {
            log.setLogName(idByToken+"修改失败");
            log.setLogStatus((byte) 0);
            userLogMapper.insertSelective(log);
        }
        return new JsonResult<>("200","修改成功");

    }
}
